package sort;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

/**
 * todo
 *
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 对象排序.java
 * @createTime 2022年09月15日 18:42:00
 */
public class 对象排序 {
    public static final String AT_LAST = "at_last";

    public static final Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);

    public static int orderCompare(List<String> value1, List<String> value2, Integer index, Comparator<Object> compare) {
        return orderCompareDescOrAsc(value1, value2, index, compare, true);
    }

    public static int orderCompareDescOrAsc(List<String> value1, List<String> value2, Integer index, Comparator<Object> compare,
                                            Boolean desc) {
        //都不为空
        if (StringUtils.isNotBlank(value1.get(index)) && StringUtils.isNotBlank(value2.get(index))) {
            //直接比较第一个维度
            int comPareResult;
            if (desc) {
                //如果指定了排在最后面
                if (value2.get(index).equals(AT_LAST) && value1.get(index).equals(AT_LAST)) {
                    comPareResult = 0;
                } else if (value1.get(index).equals(AT_LAST)) {
                    comPareResult = -1;
                } else if (value2.get(index).equals(AT_LAST)) {
                    comPareResult = 1;
                } else {
                    comPareResult = compare.compare(value1.get(index), value2.get(index));
                }
            } else {
                if (value2.get(index).equals(AT_LAST) && value1.get(index).equals(AT_LAST)) {
                    comPareResult = 0;
                } else if (value2.get(index).equals(AT_LAST)) {
                    comPareResult = -1;
                } else if (value1.get(index).equals(AT_LAST)) {
                    comPareResult = 1;
                } else {
                    comPareResult = compare.compare(value2.get(index), value1.get(index));
                }
            }
            //第一个维度相等
            if (comPareResult == 0) {
                //越界检查
                if (index == value1.size() - 1) {
                    return comPareResult;
                }
                return orderCompareDescOrAsc(value1, value2, index + 1, compare, desc);
            }
            return comPareResult;
        } else {
            //两个都是空的
            if (StringUtils.isBlank(value1.get(index)) && StringUtils.isBlank(value2.get(index))) {
                //越界检查
                if (index == value1.size() - 1) {
                    return -1;
                }
                return orderCompareDescOrAsc(value1, value2, index + 1, compare, desc);
            } else if (StringUtils.isBlank(value1.get(index))) {//第一个是空
                return desc ? 1 : -1;
            } else {//第二个空的
                return desc ? -1 : 1;
            }
        }
    }

    public static <T> List<T> genericMergeSort(List<T> list, Function<T, List<String>> function) {
        return mergeSort(list, 0, list.size() - 1, function);
    }

    public static <T> List<T> mergeSort(List<T> list, int start, int end, Function<T, List<String>> function) {
        //继续进行二分
        if (start < end) {
            int middle = (end + start) / 2;
            //左边
            mergeSort(list, start, middle, function);
            //右边
            mergeSort(list, middle + 1, end, function);
            //合并
            merge(list, start, middle, end, function);
        }
        return list;
    }

    private static <T> void merge(List<T> list, int start, int middle, int end, Function<T, List<String>> function) {
        List<T> temp = new ArrayList<>();
        int left = start;
        int right = middle + 1;
        //比较数组的左右两边
        while (left <= middle && right <= end) {
            List<String> leftList = function.apply(list.get(left));
            List<String> rightList = function.apply(list.get(right));
            if (orderCompare(leftList, rightList, 0, compare) < 1) {
                temp.add(list.get(left));
                left++;
            } else {
                temp.add(list.get(right));
                right++;
            }
        }
        //检查左边是否有多的没有排序的
        while (left <= middle) {
            temp.add(list.get(left));
            left++;
        }
        //检查右边是否还有值
        while (right <= end) {
            temp.add(list.get(right));
            right++;
        }
        int tempL = start;
        int tempListIndex = 0;
        //交换临时数组和目标数组的元素
        while (tempL <= end) {
            list.set(tempL, temp.get(tempListIndex));
            tempL++;
            tempListIndex++;
        }
    }

    public static void main(String[] args) {
        System.out.println(Integer.valueOf('3'-'0'));
       /* List<String> strings = genericMergeSort(Lists.newArrayList(
            "简称4号,别名3号,名称,规格",
            "简称4号,别名1号,名称,规格",
            "简称4号,别名5号,名称,规格",
            "简称2号,别名5号,名称,规格",
            "简称2号,别名2号,名称,规格",
            "简称2号,别名3号,名称,规格",
            "简称2号,别名3号,名称1,规格",
            "简称2号,别名3号,名称4,规格",
            "简称1号,别名1号,名称1,规格",
            "简称1号,别名1号,名称1,规格"
        ), (s) -> {
            return Lists.newArrayList(s.split(","));
        });
        strings.forEach(System.out::println);*/
    }
}
